package com.miao.util.json;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by miao on 2015/4/10.
 */
public class JsonUtils {
    private static final long serialVersionUID = 1L;

    @Deprecated
    public static String toJson(Object o) {
        ObjectMapper mapper = new ObjectMapper();
        Writer writer = null;
        String data = null;
        try {
            writer = new StringWriter();
            data = mapper.writeValueAsString(o);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return data;
    }

    @Deprecated
    public static <T> T toBean(String str, Class<T> clazz) {
        T object = null;
        ObjectMapper mapper = new ObjectMapper();
        try {
            object = mapper.readValue(str, clazz);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return object;
    }

    public static String toJson(Object o, ObjectMapper mapper) {
        Writer writer = null;
        String data = null;
        try {
            writer = new StringWriter();
            data = mapper.writeValueAsString(o);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return data;
    }

    public static <T> T toBean(String str, Class<T> clazz, ObjectMapper mapper) {
        T object = null;
        try {
            object = mapper.readValue(str, clazz);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return object;
    }

    public static void main(String[] args)
            throws Exception {
        String path = "D:\\temp\\task\\db";
        File[] files = new File(path).listFiles();
        for (File f : files) {
            String data = FileUtils.readFileToString(f);
            System.out.println(((Map) toBean(data, Map.class)).size());
        }
    }

    public static Map converKey(Map map) {
        Map result = new HashMap();
        if ((map != null) && (map.size() > 0)) {
            Iterator iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry e = (Map.Entry) iterator.next();
                String key = (String) e.getKey();
                Object val = e.getValue();
                result.put(keyToHump(key), val);
            }
        }
        return result;
    }

    private static String keyToHump(String key) {
        if (StringUtils.isNotBlank(key)) {
            String[] keys = StringUtils.split(key, "_");
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < keys.length; i++) {
                if ((StringUtils.isNotBlank(keys[i])) && (i > 0)) {
                    String first = String.valueOf(keys[i].charAt(0)).toUpperCase();
                    sb.append(first);
                    sb.append(StringUtils.substring(keys[i], 1));
                } else {
                    sb.append(StringUtils.defaultString(keys[i]));
                }
            }
            key = sb.toString();
        }
        return key;
    }
}
